// 小仓库 
const state = {
    cartList: [], //购物车列表
    orderList: [], //生成订单列表
    orderListed:[],
    edit: true,
    userAddress: [
        {
          id: 1001,
          name: '小明',
          tel: '13123456731',
          province: '黑龙江省',
          city: '哈尔滨市',
          county: '南岗区',
          addressDetail: 'asd',
          isDefault: true,
          areaCode: '110101',
          postalCode:'100001'
        },
        {
          id: 1002,
          name: '张三',
          tel: '13123456731',
          province: '黑龙江省',
          city: '哈尔滨市',
          county: '香坊区',
          addressDetail: 'asd',
          isDefault: false,
          areaCode: '110101',
          postalCode:'100002'
        },
      ],

}

const mutations = {
    AddCart(state, value) {
        state.cartList = value;
    },
    PAY(state, value) {
        state.orderList = value;
    },
    Delete(state, value) {
        state.cartList = value;
    },
    Edit(state, type) {
        if (type == 'delete') {
            state.edit = true;
        } else {
            state.edit = !state.edit;
        }
    },
    update(state){
        return state.orderListed = state.orderListed.concat(state.orderList)
    },
    change(state,value){
      state.userAddress=state.userAddress.map((item)=>{
        if(value.isDefault&&item.id!=value.id){
            item.isDefault=false
        }
        return item.id===value.id?value:item;
      })
    },
    deleteaddress(state,value){
        state.userAddress=state.userAddress.filter((item)=>{
            return value.id!=item.id
        })
        if(value.isDefault&&state.userAddress.length)
        {
            state.userAddress[0].isDefault=true
        }
    },
    onaddress(state,value){
        state.userAddress = state.userAddress.map((item)=>{
          if(value.isDefault){
            item.isDefault=false
          }
          return item;
        })
        return state.userAddress.push(value)
    }
    

}

export default {
    state,
    mutations
}